package com.web.productservice.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;

@RestController
public class HelloController {

    //SentinelResource用于配置自定义限流方法
    @SentinelResource(value = "hello",blockHandler = "helloBlockHandler",blockHandlerClass = MyBlockHandler.class)
    @RequestMapping("/hello")
    public String hello(){
        return "Hello Sentinel";
    }

    @SentinelResource(value = "order",fallback = "orderFallback",
            fallbackClass = MyFallback.class)
    @GetMapping("/order")
    public String order() {
        if(new Random().nextInt(2) == 0){
            throw new RuntimeException("订单出错了！");
        }
        System.out.println(Thread.currentThread().getName() + "等待" + System.currentTimeMillis());
        return "这是订单接口!";
    }



    @GetMapping("/payment")
    public String payment(){
        return "这是支付接口!";
    }
}